package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.constant.ApproveStatusConstant;
import com.zzl.finance.data.dto.ApproveDto;
import com.zzl.finance.data.dto.EnterprisePage;
import com.zzl.finance.data.dto.PlatEnterprisePage;
import com.zzl.finance.data.dto.VoucherEnterpriseCertificationUpdateDto;
import com.zzl.finance.data.entity.EnterpriseEntity;
import com.zzl.finance.data.service.EnterpriseService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

/**
 * @author gull
 * @date 2021-11-09 20:04:58
 */
@ApidocController("平台企业管理")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("data/platformsupplier")
@Slf4j
public class PlatformEnterpriseController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 信息
     */
    @GetMapping("/freezingStatus")
    @ApidocOperation(description = "冻结/启用", operationId = "data/supplier/info/supplierId")
    @ApidocReturnR
    public Result freezingStatus(@ApidocParamsPage(name = "params", keyName = {"id", "enterpriseFreezingStatus"},
            keyDescription = {"企业id", "企业冻结状态，启用 1 冻结"}) @RequestParam Map<String, Object> params) {
        try {
            enterpriseService.freezingStatus(params);
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 平台注册企业
     */
    @PostMapping("/platformVoucherRegister")
    @ApidocOperation(description = "企业列表-新增企业", operationId = "data/login/allowAuth/platformVoucherRegister")
    @ApidocReturnR
    public Result platformVoucherRegister(@ApidocParam(name = "保存的对象值", required = true) @RequestBody VoucherEnterpriseCertificationUpdateDto enterpriseCertificationUpdateDto) {
        log.info("平台注册企业enterpriseCertificationUpdateDto：{}", enterpriseCertificationUpdateDto);
        if (null == enterpriseCertificationUpdateDto) {
            return Result.error("供应商参数不能为空");
        }
        try {
            enterpriseService.platformVoucherRegister(enterpriseCertificationUpdateDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    /**
     * 分页查询供应商数据
     */
    @GetMapping("/platformQueryPage")
    @ApidocOperation(description = "1企业列表page", operationId = "data/supplier/list/e2")
    @ApidocReturnPage(genericTypeClass = EnterprisePage.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {"enterpriseName", "dataChannel", "category"},
            keyDescription = {"企业名称", "数据来源", "企业类别0.成员企业1.供应商，2核心企业"}) @RequestParam Map<String, Object> params) {
        PageUtils page = enterpriseService.platformQueryPage(params);
        return Result.ok().put("page", page);
    }


    /**
     * 分页查询供应商数据
     */
    @GetMapping("/platformQueryPageApprove")
    @ApidocOperation(description = "分页查询待审核企业列表", operationId = "data/supplier/list/e2")
    @ApidocReturnPage(genericTypeClass = PlatEnterprisePage.class)
    public Result platformQueryPageApprove(@ApidocParamsPage(name = "params", keyName = {"enterpriseName", "category", "businessType"},
            keyDescription = {"企业名称", "0.成员企业1.供应商，2核心企业 3 平台", "业务类型，1 注册认证，2 修改信息"})
                                               @RequestParam Map<String, Object> params) {
        PageUtils page = null;
        try {
            if(!(Integer.parseInt((String) params.get("approveStatus")) ==ApproveStatusConstant.APPROVE_STATUS_ING.getKey())){
                params.put("approveStatus", Arrays.asList(ApproveStatusConstant.APPROVE_STATUS_AGREE.getKey(),ApproveStatusConstant.APPROVE_STATUS_REFUSE.getKey()));
            }else {
                params.put("approveStatus", Collections.singletonList(ApproveStatusConstant.APPROVE_STATUS_ING.getKey()));
            }
            page = enterpriseService.listPage(params);
            return Result.ok().put("page", page);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 审核页面详情
     */
    @GetMapping("/info/{enterpriseId}")
    @ApidocOperation(description = "审核页面详情", operationId = "data/supplier/info/supplierId/aa/11")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "supplier", valueClass = VoucherEnterpriseCertificationUpdateDto.class)
    })
    public Result info(@ApidocParam(name = "enterpriseId", required = true) @PathVariable("enterpriseId") String enterpriseId) {
        VoucherEnterpriseCertificationUpdateDto supplier = null;
        try {
            supplier = enterpriseService.getInfo(enterpriseId);
            //处理名称转换
            enterpriseService.covertPage(supplier);
            return Result.ok().put("data", supplier);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }
    /**
     * 审核页面详情
     */
    @GetMapping("/infoReviewed/{enterpriseId}")
    @ApidocOperation(description = "审核页面详情已审核", operationId = "data/supplier/info/supplierId/aa/11")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "supplier", valueClass = VoucherEnterpriseCertificationUpdateDto.class)
    })
    public Result infoReviewed(@ApidocParam(name = "enterpriseId", required = true) @PathVariable("enterpriseId") String enterpriseId) {
        VoucherEnterpriseCertificationUpdateDto supplier = null;
        try {
            supplier = enterpriseService.getInfo(enterpriseId);
            //处理名称转换
            enterpriseService.covertPage(supplier);
            enterpriseService.setApproveLog(supplier);
            return Result.ok().put("data", supplier);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 企业审核
     */
    @PostMapping("/approveEnterprise")
    @ApidocOperation(description = "企业认证-平台审核", operationId = "data/supplier/approveEnterprise")
    @ApidocReturnR
    public Result approveEnterprise(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Valid ApproveDto approveDto) {
        log.info("审批入参supplier：{}", approveDto);
        try {
            enterpriseService.platApproveEnterprise(approveDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }
}
